package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;

import cn.com.api.service.*;
import cn.com.backstage.controller.base.*;

import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class ErpController  {

	private static Logger logger = Logger.getLogger(ErpController.class);
	
	@Autowired
	private ApiErpService apiErpService;  
	@Autowired
	private ApiT_productsService apiT_productsService;
	@Autowired
	private ApiGoodsService apiGoodsService;
	
	/**
	 * view
	 * @return
	 */
	@RequestMapping(value = "erp-stock-pre")
	public ModelAndView erp_stock_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		ModelAndView mv = new ModelAndView("forward:/backstage/erp_stock.jsp");
		return mv;
	}
	
	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "erp-stock-update")
	public void erp_stock_update(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		String erp_product_id = request.getParameter("erp_product_id");
		if(erp_product_id.length()>0){
			//根据商品编码同步
			List<Map<String, Object>> cur_t_products = apiT_productsService.findProductsByGoodsId(Long.valueOf(erp_product_id));
			for(Map<String, Object> product : cur_t_products){
				Object _str_erp_product_id = product.get("product_id");
				JSONObject obj = JSONObject.fromObject(product);
				ApiT_products cur_t_product  = (ApiT_products) obj.toBean(obj, ApiT_products.class);
				apiErpService.productStock4erp(cur_t_product);
			}
			//根据货品编码同步
			List<ApiT_products> cur_products = apiT_productsService.findProductsByErpProductId(erp_product_id);
			for(ApiT_products product : cur_products){
				apiErpService.productStock4erp(product);
			}
			
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "同步结束！");
		}else{
			List<ApiT_products> cur_products = apiT_productsService.findT_productss();
			for(ApiT_products product : cur_products){
				apiErpService.productStock4erp(product);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "同步所有货品结束！");
		}
		//

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "erp-order-update")
	public void erp_order_update(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		apiErpService.addOrder4erpTask();
		
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "同步结束！");
		
		//

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
}



